"use strict";

// Bomb 构造函数
function Bomb(name) {
  this.name = name;
  this.timer = null;
  this.exploded = false;
}

// 启动定时器，3秒后爆炸
Bomb.prototype.start = function() {
  if (this.exploded) {
    console.log(`[${this.name}] 炸弹已经爆炸，无法重新启动`);
    return;
  }
  
  if (this.timer) {
    console.log(`[${this.name}] 炸弹已经在运行中`);
    return;
  }
  
  console.log(`[${this.name}] 炸弹已启动，3秒后爆炸...`);
  
  this.timer = setTimeout(() => {
    this.explode();
  }, 3000);
};

// 取消定时器
Bomb.prototype.clear = function() {
  if (this.exploded) {
    console.log(`[${this.name}] 炸弹已经爆炸，无法清除`);
    return;
  }
  
  if (!this.timer) {
    console.log(`[${this.name}] 炸弹未启动，无需清除`);
    return;
  }
  
  clearTimeout(this.timer);
  this.timer = null;
  console.log(`[${this.name}] 炸弹已被清除，不会爆炸`);
};

// 爆炸方法
Bomb.prototype.explode = function() {
  this.exploded = true;
  this.timer = null;
  console.log(`💥 [${this.name}] BOOM! 炸弹爆炸了！`);
};

// 测试程序
console.log("=== 炸弹测试程序 ===");

// 实例化两个炸弹对象
const b1 = new Bomb("炸弹1");
const b2 = new Bomb("炸弹2");

console.log("启动炸弹1...");
b1.start();

console.log("启动炸弹2...");
b2.start();

console.log("1秒后清除炸弹2...");
setTimeout(() => {
  b2.clear();
}, 1000);

// 程序结束提示
setTimeout(() => {
  console.log("\n=== 测试完成 ===");
  console.log("预期结果:");
  console.log("- 炸弹1: 3秒后爆炸");
  console.log("- 炸弹2: 1秒后被清除，不会爆炸");
}, 5000);
